package com.nali.dal.statement.selectupsert;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.nali.dal.expression.ExpressionValue;
import com.nali.dal.expression.query.Criteria;
import com.nali.dal.statement.query.Selecter;
import com.nali.dal.statement.upsert.Upserter;

public class SingleSelectUpserter implements SelectUpserter {
	private static Logger logger = Logger.getLogger(SingleSelectUpserter.class);
	private Selecter selecter;
	private Upserter upserter;

	public SingleSelectUpserter(Selecter selecter, Upserter upserter) {
		super();
		this.selecter = selecter;
		this.upserter = upserter;
	}

	@Override
	public int selectUpsert(ExpressionValue<Criteria>... conds) {
		List<Map<String, Object>> rlts = selecter.execute(conds);
		for (Map<String, Object> rlt : rlts) {
			try {
				upserter.upsert(rlt);
			} catch (Exception e) {
				logger.error(e, e);
			}
		}
		return rlts.size();
	}

}